'use strict';
const dayjs = require("dayjs");

module.exports = app => {
    const { INTEGER, STRING, DATE, TINYINT, DECIMAL, TEXT } = app.Sequelize;
    const Item = app.model.define(
        'Item',
        {
            id: { type: INTEGER, primaryKey: true, autoIncrement: true },
            item_name: { type: STRING(100), allowNull: false, comment: '物品名称' },
            item_code: { type: STRING(50), allowNull: false, comment: '物品编码' },
            category_id: { type: INTEGER, allowNull: true, comment: '分类ID' },
            weight: { type: DECIMAL(10, 2), allowNull: true, comment: '重量（kg）' },
            volume: { type: DECIMAL(10, 2), allowNull: true, comment: '体积（立方米）' },
            price: { type: DECIMAL(10, 2), allowNull: true, comment: '价格' },
            description: { type: TEXT, allowNull: true, comment: '描述' },
            image: { type: STRING(255), allowNull: true, comment: '图片' },
            status: { type: TINYINT(1), defaultValue: 1, comment: '状态：0-禁用，1-启用' },
            create_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            },
            update_time: {
                type: DATE,
                get() {
                    return dayjs(this.getDataValue('update_time')).format('YYYY-MM-DD HH:mm:ss');
                }
            }
        },
        {
            tableName: 'item',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    Item.associate = function() {
        app.model.Item.belongsTo(app.model.ItemCategory, { foreignKey: 'category_id', targetKey: 'id', as: 'category' });
    };

    return Item;
};
